package com.oceansoft.mobile.biz.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.common.dao.ICommonDao;
import com.oceansoft.mobile.common.util.Result;
import com.oceansoft.mobile.weixin.service.WechatService;
import com.oceansoft.mobile.weixin.util.SignUtil;
import io.netty.util.internal.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by liujie on 2016/3/31.
 */
@Controller
@RequestMapping("api/article")
public class ApiArticleController {
    @Resource
    private WechatService wechatService;

	@Resource
	private ICommonDao commonDao;
	 
	@RequestMapping(value = "gotoPage/{articleId}")
    public String gotoPage(@PathVariable("articleId") String articleId,  HttpServletRequest request,Model model)  {
        String openId = (String) request.getSession().getAttribute("openId");
        List list = getOrder(articleId,openId);
        if(list.size()>0) {
            Map<String, Object> map = (Map<String, Object>) list.get(0);
            model.addAttribute("list", map.get("ordernum"));
        }
        String strBackUrl = "http://" + request.getServerName() //服务器地址
                + ":" 
                + request.getServerPort()           //端口号
                + request.getContextPath()      //项目名称
                + request.getServletPath()      //请求页面或其他地址
    	    + "?" + (request.getQueryString()); //参数
//    	    System.out.println(strBackUrl);
        String url = "https://wjw.szgaj.cn/szgawx/api/article/gotoPage/"+articleId;
        if(!StringUtil.isNullOrEmpty(request.getQueryString()))
        {
        	url += "?"+request.getQueryString();
        }
        System.out.println("要签名的url====="+url);
        String jsapiTicket = wechatService.getJsapiTicket();
        JSONObject json = SignUtil.getSignature(jsapiTicket, url);
        model.addAttribute("token", json);

		return "weixin/article/article_" + articleId;
    }

    /**
     * 分享
     *
     * @param request
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "shareArticle", method = POST)
    public Result submitArticleShare(HttpServletRequest request)
            throws IOException {
    	String articleId = request.getParameter("articleId");
    	String openId = (String) request.getSession().getAttribute("openId");
    	Result re = new Result("提交成功！");
    	if(!StringUtil.isNullOrEmpty(articleId))
    	{
    		//保存
    		String sql  = "insert into t_article_zan VALUES (?,?,now(),(select max(t.ordernum)+1 from t_article_zan t where t.id=? ))";
    		commonDao.updateOne(sql,articleId,openId,articleId );
    		//读取
    		List<Map> list = getOrder(articleId,openId);
    		re.setData(list.get(0));
    	}
    	else
    	{
    		re.setSucc(false);
    		re.setMsg("提交失败");
    	}
        return re;
    }
    
    private List getOrder(String articleId,String openId)
    {
    	//查询当前用户的支持情况
        String sql = "select * from t_article_zan t where t.id=? and openid=? ";
        List list  =  commonDao.loadMapList(sql,articleId,openId);
        return list;
    }
}


